<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>用户列表👴</title>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-3.4.1.min.js"></script>
    <style>
        body{margin: 10px;}w
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<div class="layui-form-item">
    <div>
        <label class="layui-form-label">用户姓名:</label>
        <div class="layui-input-inline">
            <input type="text" name="userCompellation" id="userCompellation"autocomplete="off" placeholder="请输入姓名!" class="layui-input">
        </div>
    </div>

    <div>
        <label class="layui-form-label">用户账号:</label>
        <div class="layui-input-inline">
            <input type="text" name="userName" id="userName" autocomplete="off" placeholder="用户账号!" class="layui-input">
        </div>
    </div>
    <div >
        <input type="button" class="layui-btn layui-btn-sm"id="select" value="搜索💩"></input>
    </div>
</div>
<table class="layui-hide" id="demo" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">增加💩</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除🐷</button>
    </div>

</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal"  lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del">删除</a>
</script>


<blockquote class="layui-elem-quote layui-quote-nm layui-hide" id="footer">🤣</blockquote>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>💩</legend>
</fieldset>
<script>
    layui.config({
        version: '1606406696877' //为了更新 js 缓存，可忽略
    });
    var table;
    layui.use(['laydate', 'laypage', 'layer','jquery', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
        var laydate = layui.laydate //日期
            ,laypage = layui.laypage //分页
            ,layer = layui.layer //弹层
            ,upload = layui.upload //上传
            ,element = layui.element //元素操作
            ,slider = layui.slider//滑块
            ,$=layui.$ //使用内置jQuery

        table=layui.table;

        //监听Tab切换
        element.on('tab(demo)', function(data){
            layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
                tips: 1
            });
        });

        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,id:'testReload'
            ,height: 600
            ,url: "/logisticsUser/findAll" //数据接口
            ,title: '用户表'
            ,page: true //开启分页
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'userId', title: '用户id', width:80, sort: true, fixed: 'left'}
                ,{field: 'userCompellation', title: '用户名字', width:120}
                ,{field: 'userName', title: '账号', width:120}
                ,{field: 'userPassword', title: '密码', width:120}
                ,{field: 'userPhone', title: '用户电话', width:120}
                ,{field: 'userBirthday', title: '用户生日', width:120}
                ,{field: 'userIntegral', title: '积分', width:120}
                ,{field: 'mapName', title: '地区', width:120}
                ,{field: 'userMail', title: '邮政编码', width: 120}
                ,{fixed: 'right', width: 180, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    //iframe层-父子操作
                    layer.open({
                        type: 2,
                        area: ['700px', '700px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: 'user.html'
                    });
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.msg('删除');
                        let url="/logisticsUser/deleteAll"
                        var das="";
                        for (let i=0;i<data.length;i++){
                            console.info(data[i].userId)
                            das +=data[i].userId+",";
                        }
                        var da={ids:das}
                        $.post(url,da,function (resp) {
                            if (resp.state==1){
                                layer.msg("删除成功！");
                                table.reload("testReload");
                            }else {
                                layer.msg("删除失败!");
                            }
                        },"json");
                    }

                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('🐱‍🏍', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);  //关闭弹出框
                    //向服务端发送删除指令

                    let url="/logisticsUser/delete/"+data.userId;
                    var da={id:data.userId};
                    console.log(da);
                    $.post(url,da,function (resp) {
                        if (resp.state==1){
                            layer.msg("删除成功！");
                        }else {
                            layer.msg("删除失败!");
                        }
                    },"json");
                });
            } else if(layEvent === 'edit'){
                //layer.msg('编辑操作');
                layer.open({
                    type: 2,
                    area: ['700px', '700px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: 'user.html?id='+data.userId
                });
            };


        });

        //将日期直接嵌套在指定容器中
        var dateIns = laydate.render({
            elem: '#laydateDemo'
            ,position: 'static'
            ,calendar: true //是否开启公历重要节日
            ,mark: { //标记重要日子
                '0-10-14': '生日'
                ,'2020-01-18': '小年'
                ,'2020-01-24': '除夕'
                ,'2020-01-25': '春节'
                ,'2020-02-01': '上班'
            }
            ,done: function(value, date, endDate){
                if(date.year == 2017 && date.month == 11 && date.date == 30){
                    dateIns.hint('一不小心就月底了呢');
                }
            }
            ,change: function(value, date, endDate){
                layer.msg(value)
            }
        });

        //分页
        laypage.render({
            elem: 'pageDemo' //分页容器的id
            ,count: 100 //总页数
            ,skin: '#1E9FFF' //自定义选中色值
            //,skip: true //开启跳页
            ,jump: function(obj, first){
                if(!first){
                    layer.msg('第'+ obj.curr +'页', {offset: 'b'});
                }
            }
        });

        //上传
        upload.render({
            elem: '#uploadDemo'
            ,url: 'https://httpbin.org/post' //改成您自己的上传接口
            ,done: function(res){
                layer.msg('上传成功');
                layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.files.file);
                console.log(res)
            }
        });

        //滑块
        var sliderInst = slider.render({
            elem: '#sliderDemo'
            ,input: true //输入框
        });

        //底部信息
        var footerTpl = lay('#footer')[0].innerHTML;
        lay('#footer').html(layui.laytpl(footerTpl).render({}))
            .removeClass('layui-hide');
    });

    $(function (){
        $("#select").on("click",function (){
            //执行查询
            var data={
                userCompellation:$("#userCompellation").val(),
                userName:$("#userName").val(),
            };

            //执行重载
            table.reload('testReload',{
                page: {
                    curr:1 //从第一页开始
                }
                ,where:data  //需要传到后台的参数
            });
        })
    })

</script>
</body>
</html>
